using System;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.Net;

using Styx;
using Styx.Helpers;

using TreeSharp;

namespace Loader
{
    class SwInYAH : BotBase
    {
        private readonly BotBase _botBase;

        public SwInYAH()
        {
            try
            {
                try
                {
                    // Download the DLL
                    WebClient webClient = new WebClient();


                    Logging.Write(Color.Cyan, "[SwInYAH] - Downloading SwInYAH.dll");

                    webClient.DownloadFile("http://swiny.net/ahbot/SwInYAH.dll",
                    Logging.ApplicationPath + @"\Bots\SwInYAH\SwInYAH.dll");

                    Thread.Sleep(200);

                    if (System.IO.File.Exists(Logging.ApplicationPath + @"\Bots\SwInYAH\SwInYAH.dll"))
                    {
                        Logging.Write(Color.Cyan, "[SwInYAH] - Downloaded SwInYAH.dll");
                    }
                    else
                    {
                        Logging.Write(Color.Cyan, "[SwInYAH] - Download Failed!");
                        return;
                    }
                }
                catch (Exception WebError)
                {
                    Logging.Write(Color.Red, "Error Retreiving from web.");
                    Logging.WriteDebug(WebError.ToString());
                }

                // Load the DLL
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                string path = Logging.ApplicationPath + @"\Bots\SwInYAH\SwInYAH.dll";
                Assembly asm = Assembly.LoadFrom(path);

                foreach (Type t in asm.GetTypes())
                {
                    if (t.IsSubclassOf(typeof(BotBase)) && t.IsClass)
                    {
                        var obj = Activator.CreateInstance(t);
                        _botBase = (BotBase)obj;
                    }
                }
            }
            catch (ThreadAbortException) { throw; }
            catch (Exception e)
            {
                Logging.Write(Color.Red, "Error Loading SwInYAH Check Debug log");
                Logging.WriteDebug(e.ToString());
            }
        }

        public override string Name
        {
            get { return _botBase.Name; }
        }

        public override Composite Root
        {
            get { return _botBase.Root; }
        }

        public override PulseFlags PulseFlags
        {
            get { return _botBase.PulseFlags; }
        }

        public override Form ConfigurationForm { get { return _botBase.ConfigurationForm; } }

        public override void Initialize()
        {
            _botBase.Initialize();
        }

        public override bool IsPrimaryType
        {
            get
            {
                return _botBase.IsPrimaryType;
            }
        }

        public override bool RequirementsMet
        {
            get
            {
                return _botBase.RequirementsMet;
            }
        }

        public override void Pulse()
        {
            _botBase.Pulse();
        }

        public override void Start()
        {
            _botBase.Start();
        }

        public override void Stop()
        {
            _botBase.Stop();
        }
    }
}
